package com.rp.user.controller;


import com.rp.news.common.NewsCommon;
import com.rp.news.common.PagedResult;
import com.rp.news.common.Result;
import com.rp.news.enums.Sex;
import com.rp.user.service.MyFanService;
import com.rp.vo.FansCountsVo;
import com.rp.vo.RegionRatioVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(value = "粉丝管理相关接口文档", tags = {"粉丝管理相关接口文档 api文档"})
@RequestMapping("/fans")
public class MyFansController {


    @Autowired
    private MyFanService myFanService;


    @ApiOperation(value = "查询当前用户是否关注作家", notes = "查询当前用户是否关注作家", httpMethod = "POST")
    @PostMapping("/isMeFollowThisWriter")
    public Result isMeFollowThisWriter(String writerId, String fanId) {
        return Result.ok(myFanService.isMeFollowThisWriter(writerId, fanId));
    }


    @ApiOperation(value = "用户关注作家,成为粉丝", notes = "用户关注作家,成为粉丝", httpMethod = "POST")
    @PostMapping("/follow")
    public Result follow(String writerId, String fanId) {
        boolean result = myFanService.follow(writerId, fanId);
        return Result.ok(result);
    }

    @ApiOperation(value = "用户取消关注,作家损失粉丝", notes = "用户取消关注,作家损失粉丝", httpMethod = "POST")
    @PostMapping("/unfollow")
    public Result unfollow(String writerId, String fanId) {
        myFanService.unfollow(writerId, fanId);
        return Result.ok();
    }

    @ApiOperation(value = "查询我的所有粉丝列表", notes = "查询我的所有粉丝列表", httpMethod = "POST")
    @PostMapping("/queryAll")
    public Result queryAll(String writerId, Integer page, Integer pageSize) {
        if (page == null) {
            page = NewsCommon.COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = NewsCommon.COMMON_PAGE_SIZE;
        }

        PagedResult result = myFanService.queryMyFansList(writerId, page, pageSize);

        return Result.ok(result);
    }

    @ApiOperation(value = "查询男女粉丝数量", notes = "查询男女粉丝数量", httpMethod = "POST")
    @PostMapping("/queryRatio")
    public Result queryRatio(String writerId) {
        Integer manCount = myFanService.queryFansCounts(writerId, Sex.man);
        Integer womanCount = myFanService.queryFansCounts(writerId, Sex.woman);

        FansCountsVo vo = new FansCountsVo();
        vo.setManCounts(manCount);
        vo.setWomanCounts(womanCount);
        return Result.ok(vo);
    }

    @ApiOperation(value = "根据地域查询粉丝数量", notes = "根据地域查询粉丝数量", httpMethod = "POST")
    @PostMapping("/queryRatioByRegion")
    public Result queryRatioByRegion(String writerId) {
        List<RegionRatioVO> list = myFanService.queryReginRationCounts(writerId);
        return Result.ok(list);
    }
}
